package cc.avatar.model;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

public class BusinessException extends RuntimeException {

    private static final long serialVersionUID = -5237567037176940822L;

    private static Map<String, String> errorMessages = new HashMap<String, String>();
    
    static {
    	errorMessages.put("S01", "销售单还有未被运送的货物，不能进行付款状态，请检查。");
    	errorMessages.put("S02", "销售单还有应收帐款未结清，不能关闭。");
    	errorMessages.put("S03", "销售单明细中不能存在重复的产品类型，请检查。");
    	errorMessages.put("Q01", "日期条件格式不正确，请重设。");
    }
    private String errorCode;

    private Object[] msgParams;

    public BusinessException(String errorCode) {
    	this.errorCode = errorCode;    
    }
    
    public BusinessException(String errorCode, String[] msgParams){
    	this.errorCode = errorCode;
    	this.msgParams = msgParams;
    }
    
    public BusinessException(String errorCode, String parameter){
    	this(errorCode, new String[] {parameter});
    }
    
    @Override
    public String getMessage(){
    	String resStr = errorMessages.get(this.errorCode);
        if ((resStr != null) && (msgParams != null) && (msgParams.length > 0)) {
            resStr = MessageFormat.format(resStr, msgParams);
        }
        return resStr;
    }
}

